define(function(require, exports, module) {
    require("common/dialog");
    $.extend(umei, {
	regs : function() {
	    var reg = {
		common : {
		    reg : /^\w+$/, desc : "字母,数字或下划线！"
		}, fangle : {
		    reg : /[\uFF00-\uFFFF]/, desc : "全角字符!"
		}, vcode : {
		    reg : /^\d{4}$/, desc : "4位数字！"
		}, email : {
		    reg : /^\w[\w\.-]*@[\w-]+(\.[\w-]+)+$/, desc : "邮箱格式！"
		}, idcard : {
		    reg : /^(\d{15}|\d{17}[\dx])$/, desc : "15或18位身份证号码！"
		}, chinese : {
		    reg : /^[\u4E00-\u9FAF]+$/, desc : "中文！"
		}, truename : {
		    reg : /^[\u4E00-\u9FAF]{2,4}$/, desc : "2-4个中文！"
		}, english : {
		    reg : /^[A-Za-z]+$/, desc : "英文！"
		}, date : {
		    reg : /^\d{4}-\d{2}-\d{2}$/i, desc : "公历日期(2013-07-06)！"
		}, url : {
		    reg : /^http(s)?:\/\//i, desc : "URL！"
		}, qq : {
		    reg : /^[1-9]\d{4,10}$/, desc : "5-11位QQ号！"
		}, phone : {
		    reg : /^((((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?)|(\d{11}))$/, desc : "电话或手机号码！"
		}, mobile : {
		    reg : /^(\d{1,4}\-)?(13|15|18){1}\d{9}$/, desc : "手机号码！"
		}, symbol : {
		    reg : /[`~!@#$%^&*()+=|{}':;',.<>/ ? ~！@#￥ % …… & *（）—— + |{}【】'；：""'。，、？] /, desc : "特殊字符！"
		}, password : {
		    reg : /^\w+$/, desc : "字母和数字或下划线"
		}, ip : {
		    reg : /^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$/, desc : "IP地址"
		}, mac : {
		    reg : /[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}/i, desc : "MAC地址"
		}, number : {
		    reg : /^\d+$/, desc : "数字"
		}, unnumber : {
		    reg : /\D/, desc : "不为数字"
		}, integer : {
		    reg : /^-?(0|[1-9]\d*)$/, desc : "整数！"
		}, decimal : {
		    reg : /^-?[0-9]+\.[0-9]+$/, desc : "小数"
		}
	    }
	    return reg;
	},
	/**
	 * 提示框
	 */
	tipMsg : function(config) {
	    var type = config.type;
	    var content = config.content;
	    var timeout = config.timeout;
	    var fun = config.fun;
	    var icon = "info";
	    switch (type) {
	    case "loading":
		icon = "loading";
		break;
	    case "info":
		icon = "info";
		break;
	    case "warn":
		icon = "warn";
		break;
	    case "success":
		icon = "success";
		break;
	    case "error":
		icon = "error";
		break;
	    default:
		icon = "info";
	    }
	    var content = "<div class='dialog-tip-msg'><div class='dialog-tip-msg-icon " + icon + "'></div><div class='dialog-tip-msg-content'>" + content + "</div></div>";
	    var d = dialog({
		content : content
	    });
	    if(type=="loading"){
		d.showModal();
	    }else{
		d.show();
	    }
	    if (null !== timeout && typeof (timeout) != "undefined" && timeout != 0) {
		setTimeout(function() {
		    d.remove();
		    fun == null ? "" : fun();
		}, timeout)
	    }
	    return d;
	},
	/**
	 * alert
	 */
	alert : function(content) {
	    var title = "警告";
	    var icon = "warn";
	    var content = "<div class='dialog-tip-msg'><div class='dialog-tip-msg-icon " + icon + "'></div><div class='dialog-alert-content'>" + content + "</div></div>";
	    var d = dialog({
		title : title, content : content, drag : true, cancel : false, cancelValue : '取消', okValue : '确定', ok : function() {}
	    });
	    d.showModal();
	    return d;
	},
	/**
	 * 确认框confirm
	 */
	confirm : function(msg, fun) {
	    var content = msg || "你确认要执行操作";
	    var content = "<div class='dialog-tip-msg'><div class='dialog-tip-msg-icon warn'></div><div class='dialog-confirm-content'>" + content + "</div></div>";
	    var d = dialog({
		title : "确认提示", content : content, okValue : '确定', ok : function() {
		    fun == null ? "" : fun();
		}, cancelValue : '取消', cancel : function() {}
	    });
	    d.showModal();
	    return d;
	},

	/**
	 * 弹出框
	 */
	dialog : function(config) {
	    var title = config.title
	    var content = config.content;
	    var okfun = config.okfun;
	    var cancelfun = config.cancelfun;
	    var title = title || "提示";
	    content = content || "请输入点内容吧";
	    content = "<div class='dialog-dialog-content'>" + content + "</div>";
	    var d = dialog({
		title : title, content : content, okValue : '确定', ok : function() {
		    if (okfun != null) {
			okfun();
			return false;
		    }
		}, cancelValue : '取消', cancel : function() {
		    cancelfun == null ? "" : cancelfun();
		}
	    });
	    d.showModal();
	    return d;
	},
	/**
	 * 气泡tips
	 */
	tips : function(config) {
	    var obj = config.obj;
	    var type = config.type;
	    var postion = config.postion;
	    var content = config.content;
	    var title = config.title;
	    var icon = "";
	    switch (type) {
	    case "info":
		icon = "info";
		break;
	    case "warn":
		icon = "warn";
		break;
	    case "success":
		icon = "success";
		break;
	    case "error":
		icon = "error";
		break;
	    default:
		icon = "";
	    }
	    var icondiv = "";
	    if (null == title || title == "") {
		icondiv = "<div class='dialog-tip-icon " + icon + "'></div>";
	    }
	    var content = "<div class='dialog-tip'>" + icondiv + "<div class='dialog-tip-content'>" + content + "</div></div>";

	    var d = dialog({
		align : postion, isTips : true, autofocus : false, content : content, quickClose : true, title : title
	    });
	    if (obj.length == 1) {
		obj = obj[0];
	    }
	    d.show(obj);
	    return d;
	},
	/**
	 * 分页
	 */
	pagination : function(config) {
	    var id = config.pagePanelId;
	    var page = config.pageObj;
	    var fun = config.fun;
	    // 总页数
	    var pageTotal = page.pageTotal;
	    // 当前页码
	    var curPageNo = page.page;
	    // 总记录数
	    var countTotal = page.countTotal
	    var pageNum = 10;
	    var ulPanle = $("<ul></ul>");
	    $("#" + id).empty();
	    ulPanle.appendTo($("#" + id));
	    if (pageTotal <= 1) {
		return;
	    }
	    // 起始页
	    var beginNum = 0;
	    // 结尾页
	    var endNum = 0;

	    if (pageNum > pageTotal) {
		beginNum = 1;
		endNum = pageTotal;
	    }

	    if (curPageNo - pageNum / 2 < 1) {
		beginNum = 1;
		endNum = pageNum;
	    } else {
		beginNum = curPageNo - pageNum / 2 + 1;
		endNum = curPageNo + pageNum / 2;
	    }

	    if (pageTotal - curPageNo < pageNum / 2) {
		beginNum = pageTotal - pageNum + 1;
	    }
	    if (beginNum < 1) {
		beginNum = 1;
	    }
	    if (endNum > pageTotal) {
		endNum = pageTotal;
	    }
	    if (curPageNo > 1) {
		$("<li><a href='javascript:void(0)' class='prePage'>首&nbsp;&nbsp;页</a></li>").appendTo(ulPanle).click(function() {
		    fun(1);
		});
		$("<li><a href='javascript:void(0)'><</a></li>").appendTo(ulPanle).click(function() {
		    fun((curPageNo - 1));
		});
	    } else {
		$("<li><span>首&nbsp;&nbsp;页</span></li>").appendTo(ulPanle);
		$("<li><span><</span></li>").appendTo(ulPanle);
	    }
	    for (var i = beginNum; i <= endNum; i++) {
		if (pageTotal > 1) {
		    if (i == curPageNo) {
			$("<li id='nowPage'>" + curPageNo + "</li>").appendTo(ulPanle);
		    } else {
			$("<li  pageNum=" + i + "><a href='javascript:void(0)'>" + i + "</a></li>").appendTo(ulPanle).click(function() {
			    fun($(this).attr("pageNum"));
			});
		    }
		}
	    }
	    if (curPageNo < pageTotal) {
		$("<li><a href='javascript:void(0)'>></a></li>").appendTo(ulPanle).click(function() {
		    fun((curPageNo + 1));
		});
		$("<li><a href='javascript:void(0)' class='prePage'>尾&nbsp;&nbsp;页</a></li>").appendTo(ulPanle).click(function() {
		    fun(pageTotal);
		});
	    } else {
		$("<li><span>></span></a></li>").appendTo(ulPanle);
		$("<li><span>尾&nbsp;&nbsp;页</span></li>").appendTo(ulPanle)
	    }

	    $("<li><span>总共" + countTotal + "条记录，当前" + curPageNo + "/" + pageTotal + "页</span></li>").appendTo(ulPanle)
	},
	/**
	 * ajax请求
	 */
	postRequest : function(config) {
	    var async = config.asycn;
	    var url = config.url;
	    var data = config.data || "";
	    var fun = config.fun;
	    var d = this.tipMsg({
		type : "loading", content : "加载中......"
	    });
	    var that = this;
	    $.ajax({
		async : async, cache : false, type : 'POST', dataType : "json", data : data, url : url,
		// 请求的action路径
		success : function(response) {
		    d.remove();
		    if (response.responseCode.code == umei.resultCode.LOGINTIMEOUT) { // 超时
			document.location.href = "../login.do"
		    } else if (response.responseCode.code == umei.resultCode.SUCCESS) { // 请求成功
			fun(response);
		    } else if (response.responseCode.code == umei.resultCode.NOPERMISSION) { // 没有权限
			that.tipMsg({
			    type : "error", content : response.errorMsg, timeout : 3000
			});
		    } else if (response.responseCode.code == umei.resultCode.CODEERROR) { // 验证码错误
			that.tipMsg({
			    type : "error", content : response.errorMsg, timeout : 3000
			});
			$("#refreshCode").find("img").attr("src", "verifyCode.do?" + new Date().getTime());
		    } else { // 错误
			that.tipMsg({
			    type : "error", content : response.errorMsg, timeout : 3000
			});
		    }
		}
	    });
	},
	/**
	 * form表单
	 */
	initForm : function(config) {
	    var _this = this;
	    var form = config.formObj;
	    var fun = config.fun;
	    var type = config.type;
	    var autocommit = config.autocommit;
	    var commiturl = config.commiturl;
	    form = $(form);
	    // 防止点击submit提交
	    form.attr("onsubmit", "return false");
	    var inputs = form.find("input");
	    var textareas = form.find("textarea");
	    for (var i = 0, _len = inputs.length; i < _len; i++) {
		initInfo(inputs.eq(i));
	    }
	    for (var i = 0, _len = textareas.length; i < _len; i++) {
		initInfo(textareas.eq(i));
	    }
	    bindEvent4Input(inputs);
	    bindEvent4Input(textareas);

	    if (autocommit == "true") {
		form.find("input[type='submit']").click(function() {
		    submitForm({
			formObj : form, commiturl : commiturl, fun : fun, type : type
		    });
		});
		document.onkeydown = function(event) {
		    e = event ? event : (window.event ? window.event : null);
		    if (e.keyCode == 13) {
			submitForm({
			    formObj : form, commiturl : commiturl, fun : fun, type : type
			});
		    }
		}
	    }
	    function submitForm(config) {
		var form = config.formObj;
		var fun = config.fun;
		var type = config.type;
		var commiturl = config.commiturl;
		if (commiturl == null || commiturl == "") {
		    return;
		}
		if (_this.verification({
		    formObj : form, type : type
		})) {
		    var data = _this.serialize(form);
		    _this.postRequest({
			async : true, url : commiturl, data : data, fun : function(response) {
			    if (fun != null) {
				fun(response);
			    }
			}
		    });
		}
	    }

	    function initInfo(input) {
		if (input.attr("info") != null && (input.val() == "" || input.val() == input.attr("info"))) {
		    if (input.attr("type") == "password") {
			input.attr("type", "text");
			input.attr("oldtype", "password");
		    }
		    input.val(input.attr("info"));
		    input.addClass("info-style");
		}
	    }

	    function bindEvent4Input(input) {
		input.focus(function() {
		    if ($(this).attr("oldtype") != null) {
			$(this).attr("type", $(this).attr("oldtype"))
		    }
		    $(this).removeClass("info-style");
		    $(this).removeClass("warn-style");
		    if ($(this).val() == $(this).attr("info")) {
			$(this).val("");
		    }
		});
		input.blur(function() {
		    if ($.trim($(this).val()) == "") {
			if ($(this).attr("type") == "password") {
			    $(this).attr("type", "text");
			}
			$(this).addClass("info-style");
			$(this).val($(this).attr("info"));
		    }
		});
	    }
	},
	/**
	 * 序列化
	 */
	serialize : function(form) {
	    var inputs = form.find("input");
	    var textareas = form.find("textarea");
	    // 清除提示信息
	    for (var i = 0, _len = inputs.length; i < _len; i++) {
		if (inputs.eq(i).val() == inputs.eq(i).attr("info")) {
		    inputs.eq(i).val("");
		}
	    }
	    for (var i = 0, _len = textareas.length; i < _len; i++) {
		if (inputs.eq(i).val() == inputs.eq(i).attr("info")) {
		    inputs.eq(i).val("");
		}
	    }
	    var result = form.serialize();

	    // 还原提示信息
	    for (var i = 0, _len = inputs.length; i < _len; i++) {
		if (inputs.eq(i).val() == "") {
		    inputs.eq(i).val(inputs.eq(i).attr("info"));
		}
	    }
	    for (var i = 0, _len = textareas.length; i < _len; i++) {
		if (inputs.eq(i).val() == "") {
		    inputs.eq(i).val(inputs.eq(i).attr("info"));
		}
	    }

	    return result;
	},
	/**
	 * 校验
	 */
	verification : function(config) {
	    var form = config.formObj;
	    var type = config.type;
	    $(".small-follow").remove();
	    var inputs = form.find("input");
	    var verifResult = true;
	    for (var j = 0, _jlen = inputs.length; j < _jlen; j++) {
		var inputobj = inputs.eq(j);
		var opts = inputobj.attr("opt");
		if (opts != null) {
		    var optsArray = opts.split(" ");
		    var value = $.trim(inputobj.val());
		    // 提示信息初始化的value视为空
		    if (value == inputobj.attr("info")) {
			value = "";
		    }
		    for (var i = 0, _len = optsArray.length; i < _len; i++) {
			var opt = optsArray[i];
			var checkResult = doVerification(value, opt, this.regs());
			if (checkResult == null) {
			    continue;
			}
			if (!checkResult.result) {
			    if (type == "inline") {
				inputobj.addClass("warn-style")
			    } else {
				this.tips({
				    obj : inputobj, type : "warn", postion : "right", content : checkResult.msg
				});
			    }
			    verifResult = false;
			    continue;

			}
		    }
		}
	    }
	    var checkbox_groups = form.find(".checkbox-group");
	    for (var i = 0, _len = checkbox_groups.length; i < _len; i++) {
		var checkbox_group = checkbox_groups.eq(i);
		var infomsg = checkbox_group.attr("info");
		infomsg = infomsg == null ? "请至少选择一项" : infomsg;
		if (checkbox_group.attr("opt") != null) {
		    var checkboxs = checkbox_group.find("input:checked");
		    if (checkboxs.length == 0) {
			this.tips({
			    obj : checkbox_group, type : "warn", postion : "top", content : infomsg
			});
			verifResult = false;
		    }
		}
	    }

	    function doVerification(value, opt, regs) {
		var regObj = regs[opt];
		if (opt != "rq") {
		    if (value != "" && !regObj.reg.test(value)) {
			return {
			    result : false, msg : regObj.desc
			};
		    }
		} else {
		    if (value == "") {
			return {
			    result : false, msg : "该项为必填选"
			};
		    }
		}
		return {
		    result : true
		};
	    }
	    return verifResult;
	}, tag : function(config) {
	    var id = config.id;
	    var contantClass = config.contantClass;
	    var event = config.event;
	    var fun = config.fun || function() {};
	    var tag = $("#" + id);
	    var lis = tag.find("li");
	    $("." + config.contantClass).css({
		"paddingTop" : "10px"
	    });
	    $("." + contantClass).hide();
	    $("." + contantClass).eq(0).show();
	    lis.each(function(i, v) {
		$(this).attr("index", i);
	    });
	    lis.bind(event, function() {
		lis.removeClass("active");
		$(this).addClass("active");
		$("." + config.contantClass).hide();
		$("." + config.contantClass).eq($(this).attr("index")).show();
		fun($(this).attr("index"));
	    });
	}, menuswitch : function(config) {
	    var showEffect = config.showEffect == null ? true : config.showEffect
	    var id = config.id;
	    var event = "click";
	    var fun = config.fun || function() {};
	    var menu_switch = $("#" + id);
	    var items = menu_switch.find(".menu-switch-item");
	    items.eq(0).addClass("active");
	    items.eq(0).find(".menu-switch-con").show();
	    items.each(function(i, v) {
		$(this).attr("index", i);
	    });
	    var item_tags = menu_switch.find(".menu-tag");
	    item_tags.bind(event, function() {
		var curActive = menu_switch.find(".active");
		curActive.removeClass("active");
		$(this).parent().addClass("active");
		if (showEffect) {
		    curActive.find(".menu-switch-con").slideUp();
		    $(this).parent().find(".menu-switch-con").slideDown();
		} else {
		    curActive.find(".menu-switch-con").hide();
		    $(this).parent().find(".menu-switch-con").show();
		}
		fun($(this).parent().attr("index"));
	    });
	}
    })
});